package wusiyuan.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import entity.User;
import wusiyuan.service.ChangePasswordService;
import wusiyuan.service.impl.ChangePasswordServiceImpl;


public class ChangePasswordServlet extends HttpServlet {
	ChangePasswordService cps = new ChangePasswordServiceImpl();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String username = request.getParameter("username");
		Enumeration<String> names = request.getParameterNames();
		List<String> list4 = new ArrayList<String>();
		while (names.hasMoreElements()) {
			String pName = (String) names.nextElement();
			// getParameterValues() 获取同一个参数名的多个参数值，返回字符串数组对象
			String pValues[] = request.getParameterValues(pName);
			for (int i = 0; i < pValues.length; i++) {
				list4.add(pValues[i]);
			}
		}
		List<String> list3 = new ArrayList<String>();
		List<User> list = cps.getHistoryPassword(username);
		List list2 = cps.getUserInfo(username);
		System.out.println(list2);
		list3.add(username);
		if (list != null) {
			for (User user : list) {
				list3.add(user.getPassword().toString());
			}
		}
		if (list != null) {
			for (Object object : list2) {
				list3.add(object.toString());
			}
		}
		for (Object object : list3) {
			System.out.println(object);
		}
		int i=0;
		for (String object : list3) {
			for (Object object2 : list4) {
				if (object.equals(object2)) {
					i=i+1;
				}
			}
		}
		System.out.println(i);
		System.out.println(list4.size());
		double percent=(double)i/(double)list4.size();
		System.out.println(percent);
		if(percent>0.5) {
			request.setAttribute("username", username);
			request.getRequestDispatcher("UpdateThePassword.jsp").forward(request, response);
		}	
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		doGet(request, response);
	}

}
